package com.lashou.chunshen.lashoutuangou.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;

import com.lashou.chunshen.lashoutuangou.bean.HomeCategory;
import com.lashou.chunshen.lashoutuangou.widget.MyGridView;
import com.lashou.chunshen.lashoutuangou.widget.verticalviewpager.VerticalViewPager;

import java.util.List;

/**
 * Created by momo on 2016/9/7.
 */

public class FenleiPageAdapter extends PagerAdapter {
    int pageSize = 8;
    LayoutInflater inflater;
    List<HomeCategory.ResultsBean> data;
    Context context;
    ViewPager viewpager;
    int size;

    public FenleiPageAdapter(Context context, List<HomeCategory.ResultsBean> data) {
        this.data = data;
        this.context = context;
        size = (int) Math.ceil(data.size() * 1.0 / pageSize);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (data != null) {
            //第一处修改，设置轮播最大值，等于无限循环
            return Integer.MAX_VALUE;
        }
        return 0;
    }

    /**
     * 判断对象是否生成界面
     *
     * @param view
     * @param object
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //第二处修改，当前要显示的数据索引为集合长度
        int newPosition = position % size;
        MyGridView gv = new MyGridView(context);
        GridView.LayoutParams lParams = new GridView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                AbsListView.LayoutParams.WRAP_CONTENT);
        gv.setPadding(60, 20, 0, 0);
        gv.setLayoutParams(lParams);
        gv.setNumColumns(4);
        gv.setHorizontalSpacing(0);
        gv.setVerticalSpacing(15);
        gv.setAdapter(new GridViewAdapter(context, data, newPosition));
        container.addView(gv);
        return gv;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public void setViewpager(ViewPager viewpager) {
        this.viewpager = viewpager;
    }

    public void openAutoLoop() {
        final boolean[] isLooper = new boolean[1];
        //开启一个线程，用于循环
        new Thread(new Runnable() {
            @Override
            public void run() {
                isLooper[0] = true;
                while (isLooper[0]) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //更新UI
                            viewpager.setCurrentItem(viewpager.getCurrentItem() + 1);
                        }
                    });
                }

            }
        }).start();
    }
}
